package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.example.mapper.DishBasicMapper;
import org.example.mapper.DishFlavorMapper;
import org.example.model.common.dtos.dish.DishFlavorDto;
import org.example.model.dish.DishFlavor;
import org.example.service.DishFlavorService;
import org.example.service.UserBasicService;
import org.example.utils.UserIdThreadLocalUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜品口味管理
 */
@Service
@Slf4j
@Transactional
public class DishFlavorServiceImpl extends ServiceImpl<DishFlavorMapper, DishFlavor> implements DishFlavorService {

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    /**
     * 新增菜品口味
     *
     * @param dishFlavor
     * @param dishId
     */
    @Override
    public Boolean InsertDishFlavor(DishFlavor dishFlavor, int dishId) {
        //获取Token中的数据
        Integer userId = UserIdThreadLocalUtils.getUser();
        if (userId <= 0) return false;
        //插入初始数据
        dishFlavor.setDishId(dishId);
        dishFlavor.setCreateUser(userId);
        dishFlavor.setCreateDate(LocalDateTime.now());
        dishFlavor.setUpdateUser(userId);
        dishFlavor.setUpdateDate(LocalDateTime.now());
        dishFlavor.setIsDeleted(false);
        return dishFlavorMapper.insert(dishFlavor) > 0;
    }

    /**
     * 根据DishId查询菜品口味列表
     *
     * @param dishId
     */
    @Override
    public List<DishFlavor> SelectFlavorListByDishId(int dishId) {
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishId)
                .eq(DishFlavor::isIsDeleted,false);
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(queryWrapper);
        return dishFlavors;
    }

    /**
     * 根据DishId删除菜品口味列表
     *
     * @param dishId
     */
    @Override
    public Boolean DeleteFlavorListByDishId(int dishId) {
        //获取Token中的数据
        Integer userId = UserIdThreadLocalUtils.getUser();
        if (userId <= 0) return false;
        List<DishFlavor> dishFlavors = this.SelectFlavorListByDishId(dishId);
        if (dishFlavors == null || dishFlavors.size() <= 0) return true;
        dishFlavors = dishFlavors.stream().map(item -> {
            item.setIsDeleted(true);
            item.setUpdateUser(userId);
            item.setUpdateDate(LocalDateTime.now());
            return item;
        }).collect(Collectors.toList());
        for (DishFlavor dishFlavor : dishFlavors) {
            Boolean flag = dishFlavorMapper.updateById(dishFlavor) > 0;
            if(!flag) return false;
        }
        return true;
    }
}
